package com.goose.bmm.controler.adapter;

import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.Spinner;
import android.widget.TextView;

import com.goose.bmm.R;
import com.goose.bmm.model.entity.Player;

public class ListPlayerAdapter extends BaseAdapter {

	private ArrayList<Player> listPlayer;
	private Activity mActivity;
	private LayoutInflater mInflater;
	private int counter_first_team;

	public ListPlayerAdapter(Activity activity, ArrayList<Player> listPlayer) {
		super();
		this.listPlayer = listPlayer;
		mActivity = activity;
		mInflater = (LayoutInflater) mActivity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		counter_first_team = 0;
	}

	public int getCounterFisrtTeam() {
		return counter_first_team;
	}

	@Override
	public int getCount() {
		return listPlayer.size();
	}

	@Override
	public Object getItem(int position) {
		return listPlayer.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.item_list_player, null);
			holder = new ViewHolder();
			holder.mTextViewFullName = (TextView) convertView
					.findViewById(R.id.tv_full_name);
			holder.mTextViewNumber = (TextView) convertView
					.findViewById(R.id.tv_number);
			holder.mTextViewPosition = (TextView) convertView
					.findViewById(R.id.tv_position);
			holder.mTextViewAge = (TextView) convertView
					.findViewById(R.id.tv_age);
			holder.mCheckBoxFirstTeam = (CheckBox) convertView
					.findViewById(R.id.cb_first_team);
			holder.mCheckBoxFirstTeam.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					CheckBox checkBox = (CheckBox) v;
					if (checkBox.isChecked()) {
						if (counter_first_team < 5) {
							counter_first_team++;
							listPlayer.get(position).setInCourt(true);
						} else {
							new AlertDialog.Builder(mActivity)
									.setMessage(
											mActivity
													.getResources()
													.getString(
															R.string.dialog_massage_first_team_full))
									.setPositiveButton(
											mActivity.getResources().getString(
													R.string.ok), null).show();
							checkBox.setChecked(false);
						}
					} else {
						listPlayer.get(position).setInCourt(false);
						counter_first_team--;
					}
				}
			});

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		Player playerTMP = listPlayer.get(position);
		holder.mTextViewFullName.setText((position + 1) + ". "
				+ playerTMP.getName());
		holder.mTextViewNumber.setText(mActivity.getResources().getString(
				R.string.tv_player_number)
				+ ": " + playerTMP.getNumber());
		holder.mTextViewPosition.setText(mActivity.getResources().getString(
				R.string.tv_player_position)
				+ ": " + playerTMP.getPositions());
		holder.mTextViewAge
				.setText(mActivity.getResources().getString(
						R.string.tv_player_age)
						+ ": "
						+ (1900 + Calendar.getInstance().getTime().getYear() - playerTMP
								.getBirthYear()) + "");
		
		if(playerTMP.isInCourt()) {
			holder.mCheckBoxFirstTeam.setChecked(true);
		}
		
		return convertView;
	}

	static class ViewHolder {
		TextView mTextViewFullName;
		TextView mTextViewNumber;
		TextView mTextViewPosition;
		TextView mTextViewAge;
		CheckBox mCheckBoxFirstTeam;
	}

}
